package com.hnhd.eorder.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.hnhd.eorder.utils.FormatUtil;
import com.hnhd.eorder.vo.Order;

/**
 * 订单数据操作类
 * 
 * @author peng
 *
 */
public class OrderUtil {

	public static Order getOrderById(String id) {
		Cursor cursor = SQLUtil.query(SQLUtil.ORDER, null, "id=?", new String[] { id }, null,
				null, null);
		Order order = null;
		if (cursor.moveToFirst()) {
			do {
				order = new Order();
				order.setId(cursor.getInt(0));
				order.setTableID(cursor.getInt(1));
				order.setNumber(cursor.getInt(2));
				order.setServer(cursor.getString(3));
				order.setCashier(cursor.getInt(4));
				order.setPrice(cursor.getFloat(5));
				order.setDiscount(cursor.getFloat(6));
				order.setStatus(cursor.getString(7));
				order.setRemarks(cursor.getString(8));
				order.setOrderTime(FormatUtil.convertDate(cursor.getString(9)));
				order.setEndTime(FormatUtil.convertDate(cursor.getString(10)));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return order;
	}


	public static ArrayList<Order> getAllOrders() {
		Cursor cursor = SQLUtil.getReadableDatabase().rawQuery("SELECT * FROM " + SQLUtil.ORDER,
				null);
		ArrayList<Order> orders = new ArrayList<Order>();
		if (cursor.moveToFirst()) {
			do {
				Order order = new Order();
				order.setId(cursor.getInt(0));
				order.setTableID(cursor.getInt(1));
				order.setNumber(cursor.getInt(2));
				order.setServer(cursor.getString(3));
				order.setCashier(cursor.getInt(4));
				order.setPrice(cursor.getFloat(5));
				order.setDiscount(cursor.getFloat(6));
				order.setStatus(cursor.getString(7));
				order.setRemarks(cursor.getString(8));
				order.setOrderTime(FormatUtil.convertDate(cursor.getString(9)));
				order.setEndTime(FormatUtil.convertDate(cursor.getString(10)));
				orders.add(order);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return orders;
	}

	public static long insertOrder(Order order) {
		ContentValues values = new ContentValues();
		values.put("id", order.getId());
		values.put("tableId", order.getTableID());
		values.put("number", order.getNumber());
		values.put("server", order.getServer());
		values.put("cashier", order.getCashier());
		values.put("price", order.getPrice());
		values.put("discount", order.getDiscount());
		values.put("status", order.getStatus());
		values.put("remarks", order.getRemarks());
		values.put("orderTime", order.getOrderTime().toString());
		values.put("endTime", order.getEndTime().toString());
		return SQLUtil.getWritableDatabase().insert(SQLUtil.ORDER, null, values);
	}

	public static long updateOrder(Order order) {
		ContentValues values = new ContentValues();
		values.put("id", order.getId());
		values.put("tableId", order.getTableID());
		values.put("number", order.getNumber());
		values.put("server", order.getServer());
		values.put("cashier", order.getCashier());
		values.put("price", order.getPrice());
		values.put("discount", order.getDiscount());
		values.put("status", order.getStatus());
		values.put("remarks", order.getRemarks());
		values.put("orderTime", order.getOrderTime().toString());
		values.put("endTime", order.getEndTime().toString());
		return SQLUtil.getWritableDatabase().update(SQLUtil.ORDER,values,"id=?",new String[]{String.valueOf(order.getId())});
	}

	public static void deleteOrder(Order order) {
		SQLUtil.getWritableDatabase().delete(SQLUtil.ORDER , "id=?" , new String[]{String.valueOf(order.getId())});
	}
}
